/***
 * 这里存放commander 命令的 所有action 步骤
 */
const { promisify } = require('util')
const path = require('path')
const clone = require('./download.js')
const Repos = require('../config/repo-config.js')
const { writeToFile, createDirSync, log } = require('../utils/util')
const { commandSpawn } = require('../utils/terminals.js')
const inquirer = require('inquirer'); //选择模版模块

// 创建Vue2.7模版
const createProjectVue = async (pro) => {
    //选择模版模块
    let answer = await inquirer.prompt([{
        type: "list",
        name: "tmp",
        message: "请选择要创建的模版",
        choices: ['vue', 'uni', 'admin', 'v3', 'v3tsVite', 'v3admin', 'vite'],
    }])

    let dir = path.resolve(`./${pro.toLowerCase()}`); //toLowerCase()转小写
    //1.clone 项目 //repo 克隆地址  //dir 克隆到什么地方
    log(`创建项目 ${pro}`)
    await clone(Repos[`${answer.tmp}Repo`], dir)

    if (answer.tmp == 'vue' || answer.tmp == 'admin' || answer.tmp == 'v3' || answer.tmp == 'v3tsVite' || answer.tmp == 'vite' ) { //判断是都是vue模版
        //2.安装依赖 npm install ;  cwd :路径
        // process.platform判断系统执行命令
        log('安装依赖...')
        const command = process.platform === 'win32' ? 'npm.cmd' : 'npm';
        await commandSpawn(command, ['install'], { cwd: `./${pro}` })
        //3.启动命令
        log('启动中...')
        if (answer.tmp == 'vite' || answer.tmp == 'v3tsVite') {
            await commandSpawn('yarn', ['dev'], { cwd: `./${pro}` })
        }else{
            await commandSpawn(command, ['run', 'serve'], { cwd: `./${pro}` })
        }
        
    }

}






module.exports = {
    createProjectVue
}